#include "EndScene.h"
#include "StartScene.h"
USING_NS_CC;
using namespace ui;
Scene* EndScene::createScene()
{
    // 'scene' is an autorelease object
	auto scene = Scene::create();

	// 'layer' is an autorelease object
	auto layer = EndScene::create();

	// add layer as a child to scene
	scene->addChild(layer);

	// return the scene
	return scene;
}
bool EndScene::init()
{

	Size s = Director::getInstance()->getVisibleSize();
	//结束背景
	auto endbackground = Sprite::create("res/end.png");
	endbackground->setScale(s.width / 900);
	endbackground->setPosition(Point(s.width / 2, s.height / 2));
	this->addChild(endbackground);

	//返回按钮
	Button* back = Button::create();
	back->setTouchEnabled(true);
	back->loadTextures("res/return.png", "res/return.png", "");
	back->setScale(s.width / 900);
	back->setPosition(Point(s.width / 2, s.height *4/ 9));
	back->addTouchEventListener(this, toucheventselector(EndScene::BacktouchEvent));
	this->addChild(back);

	initScor();
	scheduleUpdate();
	return true;
}
//分数显示
void EndScene::initScor()
{
	Size s = Director::getInstance()->getVisibleSize();
	for (int i = 0; i < 5; i++)
	{
		scor[i] = Sprite::create("res/score_.png", Rect(0, 0, 40, 40));
		scor[i]->setScale(s.width / 900);
		scor[i]->setPosition(Point(s.width / 2 - 125 * s.width / 900 + 50 * i*s.width / 900 + scor[i]->getBoundingBox().size.width / 2, s.height * 11 / 16));
		this->addChild(scor[i]);
	}
	
}

void EndScene::update(float dt)
{
	if (score<10)
	{
		scor[4]->setTexture("res/score_.png");
		scor[4]->setTextureRect(Rect(score * 40, 0, 40, 40));
	}
	if (score >= 10 && score<100)
	{
		int s = score / 10;
		int g = score - s * 10;
		scor[4]->setTexture("res/score_.png");
		scor[4]->setTextureRect(Rect(g * 40, 0, 40, 40));
		scor[3]->setTexture("res/score_.png");
		scor[3]->setTextureRect(Rect(s * 40, 0, 40, 40));

	}
	if (score >= 100 && score<1000)
	{
		int q = score / 100;
		int s = (score - q * 100) / 10;
		int g = score - q * 100 - s * 10;
		scor[4]->setTexture("res/score_.png");
		scor[4]->setTextureRect(Rect(g * 40, 0, 40, 40));
		scor[3]->setTexture("res/score_.png");
		scor[3]->setTextureRect(Rect(s * 40, 0, 40, 40));
		scor[2]->setTexture("res/score_.png");
		scor[2]->setTextureRect(Rect(q * 40, 0, 40, 40));

	}
	if (score >= 1000 && score < 10000)
	{
		int w = score / 1000;
		int q = (score - w * 1000) / 100;
		int s = (score - w * 1000 - q * 100) / 10;
		int g = score - w * 1000 - q * 100 - s * 10;
		scor[4]->setTexture("res/score_.png");
		scor[4]->setTextureRect(Rect(g * 40, 0, 40, 40));
		scor[3]->setTexture("res/score_.png");
		scor[3]->setTextureRect(Rect(s * 40, 0, 40, 40));
		scor[2]->setTexture("res/score_.png");
		scor[2]->setTextureRect(Rect(q * 40, 0, 40, 40));
		scor[1]->setTexture("res/score_.png");
		scor[1]->setTextureRect(Rect(w * 40, 0, 40, 40));

	}
	if (score >= 10000 && score < 100000)
	{
		int sw = score / 10000;
		int w = (score - sw * 10000) / 1000;
		int q = (score - sw * 10000 - w * 1000) / 100;
		int s = (score - sw * 10000 - w * 1000 - q * 100) / 10;
		int g = score - sw * 10000 - w * 1000 - q * 100 - s * 10;
		scor[4]->setTexture("res/score_.png");
		scor[4]->setTextureRect(Rect(g * 40, 0, 40, 40));
		scor[3]->setTexture("res/score_.png");
		scor[3]->setTextureRect(Rect(s * 40, 0, 40, 40));
		scor[2]->setTexture("res/score_.png");
		scor[2]->setTextureRect(Rect(q * 40, 0, 40, 40));
		scor[1]->setTexture("res/score_.png");
		scor[1]->setTextureRect(Rect(w * 40, 0, 40, 40));
		scor[0]->setTexture("res/score_.png");
		scor[0]->setTextureRect(Rect(sw * 40, 0, 40, 40));

	}
}

void EndScene::BacktouchEvent(cocos2d::Object *pSender, cocos2d::ui::TouchEventType type)
{
	Button* butten = (Button*)pSender;
	unsigned int tag = butten->getTag();
	switch (type)
	{
	case ui::TOUCH_EVENT_BEGAN:
		butten->setPositionY(butten->getPositionY() - 10);
		break;

	case ui::TOUCH_EVENT_MOVED:

		break;

	case ui::TOUCH_EVENT_ENDED:
		butten->setPositionY(butten->getPositionY() + 10);

		Director::getInstance()->replaceScene(TransitionFade::create(1.2f, StartScene::createScene()));


		break;

	case ui::TOUCH_EVENT_CANCELED:
		butten->setPositionY(butten->getPositionY() + 10);
		break;

	default:
		break;
	}
}
EndScene::~EndScene()
{
	
}